package com.spider.iceCream.spiderComponent.main;

import com.spider.iceCream.spiderComponent.model.Rank;
import com.spider.iceCream.spiderComponent.model.VideoModel;
import com.spider.iceCream.spiderComponent.parse.parse;
import com.spider.iceCream.spiderComponent.util.HttpRequestUtil;

import java.util.ArrayList;
import java.util.List;

public class Crawler {
    static HttpRequestUtil httpRequestUtil = new HttpRequestUtil();

    public static void main(String[] args) throws Exception {
        //爬取排行榜
        //    List<videoModel> datalist = parse.getData(html);
        //所有排行榜数组
        String[] sname = {"all", "guochuang", "douga", "music", "dance", "game", "knowledge", "tech", "car", "life", "food", "animal", "kichiku", "fashion", "ent", "cinephile", "origin", "rookie"};
        //遍历排行榜
        for (int s = 0; s < sname.length; s++) {
            String rankUrl = "https://www.bilibili.com/v/popular/rank/" + sname[s];
            String rankHtml = httpRequestUtil.getContentByGetMethod(rankUrl, "utf-8");
            List<VideoModel> datalist = parse.getData(rankHtml);//当前排行榜信息

            List<VideoModel> VideoModels = new ArrayList<VideoModel>(); //所有排行榜信息
            VideoModels.addAll(datalist);

//            for (int i = 99; i < datalist.size(); i++) {
//                String oid = datalist.get(i).getOid();
//                System.out.println(datalist.get(i).toString());
//                List<replyModel> replyModels = getReply.allReply(oid);
//            }
        }
    }

    public List<Rank> getRank() throws Exception {
        //爬取排行榜
        //所有排行榜数组
        try {
            List<Rank> rankList = new ArrayList<Rank>();
            String[] sname = {"all", "guochuang", "douga", "music", "dance", "game", "knowledge", "tech", "car", "life", "food", "animal", "kichiku", "fashion", "ent", "cinephile", "origin", "rookie","sports"};
            //遍历排行榜
            for (int s = 0; s < sname.length; s++) {
                String rankUrl = "https://www.bilibili.com/v/popular/rank/" + sname[s];
                String rankHtml = httpRequestUtil.getContentByGetMethod(rankUrl, "utf-8");
                //当前排行榜信息
                List<VideoModel> datalist = parse.getData(rankHtml);
                //所有排行榜信息
                Rank rank = new Rank();
                rank.setCategory(sname[s]);
                rank.setRankList(datalist);
                rankList.add(rank);
            }
            return rankList;
        }catch (Exception e){
            System.out.println("hp lj:"+e);
            return null;
        }

    }
//  System.out.println("i="+i);

//
//           // 爬取评论
//            JsonParser jp = new JsonParser();
//            List<replyModel> replyModels = new ArrayList<replyModel>();
//            boolean is_end;
//            int j = 1;
//            while (true) {
//                String replyUrl = "https://api.bilibili.com/x/v2/reply/main?jsonp=jsonp&next=" + j + "&type=1&oid=" + oid + "&mode=3&plat=1";
//                String replyHtml = httpRequestUtil.getContentByGetMethod(replyUrl, "utf-8");
//                JsonObject jo = jp.parse(replyHtml).getAsJsonObject();
//              //  System.out.println(j);
//              //  System.out.println(replyUrl);
//                is_end = jo.get("data").getAsJsonObject().get("cursor").getAsJsonObject().get("is_end").getAsBoolean();
//                if (is_end != false) break;
//                replyModels.addAll(getReply.reply(replyHtml));
//                j++;
//                Thread.sleep(100);
//            }
//            for (replyModel rm : replyModels) {
//                System.out.println(rm.toString());
//            }
//        }
}
